const app = getApp()

Page({
  data: {
    banner: [],
    active: 0,
    list: [],
    typeData: [],
    page: 1,
    count: 20,
    type: "All"
  },
  onLoad(query) {
    const self = this
    app.http({
      url: "https://gank.io/api/v2/banners"
    }).then(res => {
      self.setData({
        banner: res.data.data
      })
      // console.log(self.data.banner)
    })
    app.http({
      url: "https://gank.io/api/v2/categories/Article"
    }).then(res => {
      self.setData({
        typeData: res.data.data
      })
      console.log(self.data.typeData)
    })

    this.getList({
      count: self.data.count
    })
  },
  onClick(event) {
    const { index, type } = event.currentTarget.dataset
    this.setData({
      active: index,
      type,
      page: 1,
      list: []
    })

    this.getList({
      type: type,
      page: 1,
      count: this.data.count
    })
  },
  getList(options = {}) {
    const self = this
    let page = options.page || 1
    let count = options.count || 10
    let type = options.type || "All"
    wx.showLoading({title: "Loading..."})
    console.log(`https://gank.io/api/v2/data/category/All/type/${type}/page/${page}/count/${count}`)
    app.http({
      url: `https://gank.io/api/v2/data/category/All/type/${type}/page/${page}/count/${count}`
    }).then(res => {
      self.setData({
        list: [...self.data.list, ...res.data.data]
      })
      // console.log(self.data.list)
    }).finally(() => {
      wx.hideLoading()
    })
  },
  onReachBottom() {
    const self = this
    self.setData({ page: self.data.page + 1 })
    console.log("到底了", self.data.page)
    this.getList({
      page: self.data.page,
      count: self.data.count,
      type: self.data.type,
    })
  },
  toArticle(event) {
    const { id } = event.currentTarget.dataset
    wx.navigateTo({
      url: `../article/article?id=${id}`
    })
  }
})
